/*******************************************************************************
 * Copyright (c) 2012-2016 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package org.eclipse.che.plugin.docker.client.connection;

import com.google.inject.Inject;

import org.eclipse.che.plugin.docker.client.DockerCertificates;
import org.eclipse.che.plugin.docker.client.DockerConnector;
import org.eclipse.che.plugin.docker.client.DockerConnectorConfiguration;

import javax.inject.Named;
import java.net.URI;

/**
 * Factory for connections to docker API.
 *
 * <p>Detects connection implementation by checking docker daemon URI.
 *
 * @author Alexander Garagatyi
 */
public class DockerConnectionFactory {
    public static final String CONNECTION_TIMEOUT_MS_PROPERTY      = "docker.connection.tcp.connection_timeout_ms";
    public static final String CONNECTION_READ_TIMEOUT_MS_PROPERTY = "docker.connection.tcp.read_timeout_ms";

    @Inject(optional = true)
    @Named(CONNECTION_TIMEOUT_MS_PROPERTY)
    private int connectionTimeoutMs = 60000;

    @Inject(optional = true)
    @Named(CONNECTION_READ_TIMEOUT_MS_PROPERTY)
    private int connectionReadTimeoutMs = 60000;

    private final DockerCertificates dockerCertificates;

    @Inject
    public DockerConnectionFactory(DockerConnectorConfiguration connectorConfiguration) {
        this.dockerCertificates = connectorConfiguration.getDockerCertificates();
    }

    public DockerConnection openConnection(URI dockerDaemonUri) {
        if (DockerConnectorConfiguration.isUnixSocketUri(dockerDaemonUri)) {
            return new UnixSocketConnection(dockerDaemonUri.getPath());
        } else {
            return new TcpConnection(dockerDaemonUri, dockerCertificates, connectionTimeoutMs, connectionReadTimeoutMs);
        }
    }
}
